<template>
  <div class="login">
    <img :src="img" alt="" />
    <el-card class="box-card clearfix">
    <span class="title">影城购票管理系统</span>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password"  type="password" show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >登录</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { login } from '@/api/login'
export default {
  data () {
    const nameRules = (rule, value, callback) => {
      if (value === '') callback(new Error('用户名不能为空'))
      if (value.length < 5) {
        callback(new Error('用户名长度不能低于6,只能由数字、字母、_-组成'))
      }
      callback()
    }
    const pswRules = (rule, value, callback) => {
      if (value.length < 5) callback(new Error('密码长度不能少于5'))
      callback()
    }
    return {
      img: require('@/views/login/images/bg_admin.png'),
      ruleForm: {
        name: '',
        password: ''
      },
      rules: {
        name: [{ required: true, trigger: 'blur', validator: nameRules }],
        password: [{ required: true, trigger: 'blur', validator: pswRules }]
      }
    }
  },
  methods: {
    submitForm (formName) {
      // 再次对form表单进行验证
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          await login(this.ruleForm)
          this.$router.push('/') // 跳转到首页
        }
      })
    },
    resetForm (formName) {
      // 重置form表单
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="less">
.login {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  img {
    width: 100%;
    height: 100%;
  }
  .clearfix {
    position: absolute;
    width: 500px;
    height: 350px;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    z-index: 199;
    .title {
      display: block;
      margin: 20px auto;
      font-size: 36px;
      margin-left: 55px;
      color: rgb(148, 146, 146);
    }
  }
  .el-card__body {
    position: absolute;
    left: 44%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 400px;
    padding: 0;
  }
}
</style>
